package com.sam.im.heiqun.uis.activities.phonecontact;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sam.im.heiqun.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangguodong on 2017/6/20.
 */
public class LinkmenListAdapter extends BaseAdapter {

    private List<AllUserBean> data;
    private LayoutInflater inflater;
    private Context mContext;
    private boolean showFinish;
    private IShowMore showMore;

    public void setCallBack(IClickCallBack callBack) {
        this.callBack = callBack;
    }

    private IClickCallBack callBack;

    public LinkmenListAdapter(List<AllUserBean> data, Context mContext) {
        this.data = data != null ? data : new ArrayList<AllUserBean>();
        this.inflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
    }

    @Override
    public int getItemViewType(int position) {
        int type = 0;
        if (showFinish&& position == data.size()) {
            type = 1;
        }
        return type;
    }


    public List<AllUserBean> getData() {
        return data;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getCount() {
        return showFinish ? data.size() + 1 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return showFinish && position == data.size() ? null : data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            switch (getItemViewType(position)) {
                case 0:
                    view = inflater.inflate(R.layout.item_phone_user, viewGroup, false);
                    holder = new ViewHolder(view, getItemViewType(position));
                    view.setTag(holder);
                    break;
                case 1:
                    view = inflater.inflate(R.layout.item_show_finish, viewGroup, false);
                    holder = new ViewHolder(view, getItemViewType(position));
                    view.setTag(holder);
                    break;
            }

        } else {
            holder = (ViewHolder) view.getTag();
        }

        switch (getItemViewType(position)) {
            case 0:
               AllUserBean addressBean = data.get(position);
                if (addressBean == null) {
                    break;
                }
                String headurl = addressBean.getHeadurl();
                if (headurl != null && !"".equals(headurl)) {
//                    Glide.with(mContext).load(ConstantURL.YHLIVE+"/"+headurl).into(holder.head_image);
                }
                String nickName = addressBean.getNickName();
                Log.e("ATG","=====NickName====="+nickName);
                if(nickName!=null && !"".equals(nickName)){
                    holder.recommend_name.setText(addressBean.getNickName());
                }

                String mobile = addressBean.getMobile();
                if(mobile!=null && !"".equals(mobile)){
                    holder.recommend_phone.setText(addressBean.getMobile());
                }
                // 调用加载更多的方法
                if (position == getCount() - 1) {
                    if (showMore != null) showMore.showMore();
                }
                break;
        }

        return view;
    }


    /**
     * 增加界面数据
     */
    public void updateRes(List<AllUserBean> datas, boolean showFinish) {
        if (null!=datas){
            if (null==data){
                data = new ArrayList<>();
            }
            data.clear();
            data.addAll(datas);
            //Log.e("ATG","=====dataNickName====="+data.get(0).getNickName());
            this.showFinish = showFinish;
            notifyDataSetChanged();
        }
    }


    /**
     * 增加界面数据
     */
    public void setdateRes(List<AllUserBean> datas, boolean showFinish) {
        if (null==datas){
            data = new ArrayList<>();
        }else {
            data.addAll(datas);
        }
        this.showFinish = showFinish;
        notifyDataSetChanged();
    }

    // --------------holder--------------
    private class ViewHolder {
        ImageView head_image;
        TextView recommend_name;
        TextView recommend_phone;
        Button btnYaoqing;
//        ImageView imgHongdian;

        public ViewHolder(View view, int type) {
            switch (type) {
                case 0:
                    head_image = (ImageView) view.findViewById(R.id.img);
                    recommend_name= (TextView) view.findViewById(R.id.item_name);
                    recommend_phone = (TextView) view.findViewById(R.id.item_detail);
//                    btnYaoqing = (Button) view.findViewById(R.id.yaoqing);
//                    imgHongdian = (ImageView) view.findViewById(R.id.img_hong_dian);
                    break;
            }
        }
    }


    public void setShowFinish(boolean showFinish) {
        this.showFinish = showFinish;
    }

    public void setShowMore(IShowMore showMore) {
        this.showMore = showMore;
    }

    //----------- interface ----------
    public interface IShowMore {
        void showMore();
    }

    public interface IClickCallBack {
        void yaoQingClick(int position);
    }

}
